﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ITViet.Web.App_Code;
using ITViet.Domains;
using ITViet.Services;

namespace ITViet.Web.Admin
{
  public partial class ProCategoryDetail : AdminBasePage
  {
    #region Properties
    private string m_innerError = string.Empty;

    internal override int RequiredPermission
    {
      get
      {
        return (int)FunctionList.Product;
      }
    }

    private int HiddenId
    {
      get
      {
        int Id = 0;
        int.TryParse(hfCategoryId.Value.ToString(), out Id);
        return Id;
      }
    }
    private int LocaleId
    {
      set
      {
        hfLocaleId.Value = value.ToString();
      }
      get
      {
        int Id = 0;
        if (!string.IsNullOrEmpty(hfLocaleId.Value))
          int.TryParse(hfLocaleId.Value, out Id);
        return Id;
      }
    }

    private int CId
    {
      get
      {
        int id = 0;
        if (!string.IsNullOrEmpty(Request.QueryString["cid"].ToString()))
          int.TryParse(Request.QueryString["cid"].ToString(), out id);
        hfCategoryId.Value = id.ToString();
        return id;
      }
    }
    #endregion

    #region Events

    internal override void SetPermission()
    {
      if (!CanModify) btnSave.Visible = false;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
        LoadPage();
    }
    #endregion

    #region Methods

    private void LoadPage()
    {
      xdsLanguage.DataFile = XmlStatusFile;
      cbbLanguage.Value = CurrentLanguage.Id;
      LoadLocale(CId);
    }

    private void LoadLocale(int Id)
    {
      TList<ProCategoryLocale> pclList = new ProCategoryLocaleService().GetByCategoryId(Id);
      foreach (ProCategoryLocale item in pclList)
      {
        if (item.LanguageId == CurrentLanguage.Id)
        {
          txtName.Text = item.Name;
          hdDescription.Html = item.Description;
          this.LocaleId = item.Id;
          return;
        }
      }
    }

    private string SaveLocale()
    {
      try
      {
        ProCategoryLocale pcl = new ProCategoryLocale();
        if (HiddenId == 0)
          return "error::save_" + Resources.Messages.ERR0004;
        if (string.IsNullOrEmpty(txtName.Text.Trim()))
          return "error::save_" + Resources.Messages.ERR0004;
        pcl.CategoryId = HiddenId;
        pcl.LanguageId = (int)cbbLanguage.Value;
        pcl.Name = txtName.Text.Trim();
        pcl.Description = hdDescription.Html;
        // update
        if (this.LocaleId != 0)
          return UpdateLocale(pcl);
        // create new
        new ProCategoryLocaleService().Save(pcl);
        return "save";
      }
      catch (Exception ex)
      {
        WriteLog("ProCategoryDetail.aspx::Save ProCategoryLocale", ex);
        return "error::save";
      }
    }

    private string UpdateLocale(ProCategoryLocale p)
    {
      try
      {
        p.Id = this.LocaleId;
        new ProCategoryLocaleService().Update(p);
        return "save";
      }
      catch (Exception ex)
      {
        WriteLog("ProCategoryDetail.aspx::Update ProCategoryLocale", ex);
        return "error::save_" + Resources.Messages.ERR0004;
      }
    }

    private string ChangeLocale()
    {
      TList<ProCategoryLocale> pclList = new ProCategoryLocaleService().GetByCategoryId(CId);
      foreach (ProCategoryLocale item in pclList)
      {
        if (item.LanguageId == (int)cbbLanguage.Value)
        {
          return string.Format("[language]ok|{0}|{1}|{2}", item.Name, item.Description, item.Id);
        }
      }
      return string.Format("[language]|{0}|{1}|{2}", string.Empty, string.Empty, 0);
    }

    #endregion

    protected void ctrlCallback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {
      if (e.Parameter.StartsWith("save"))
      {
        e.Result = SaveLocale();
      }
      else if (e.Parameter.StartsWith("language"))
      {
        e.Result = ChangeLocale();
      }
    }
  }
}
